[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This chapter describes functions that return information about the particular machine that is in use—the type of hardware, the type of software, and the individual machine’s name.
1.1 Host Identification | Determining the name of the machine. | |
1.2 Hardware/Software Type Identification | Determining the hardware type of the machine and what operating system it is running. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section explains how to identify the particular machine that your program is running on. The identification of a machine consists of its Internet host name and Internet address; see @ref{Internet Namespace}. The host name should always be a fully qualified domain name, like ‘crispy-wheats-n-chicken.ai.mit.edu’, not a simple name like just ‘crispy-wheats-n-chicken’.
Prototypes for these functions appear in ‘unistd.h’. The shell
commands hostname
and hostid
work by calling them.
This function returns the name of the host machine in the array name. The size argument specifies the size of this array, in bytes.
The return value is 0
on success and -1
on failure. In
the GNU C library, gethostname
fails if size is not large
enough; then you can try again with a larger array. The following
errno
error condition is defined for this function:
ENAMETOOLONG
The size argument is less than the size of the host name plus one.
On some systems, there is a symbol for the maximum possible host name
length: MAXHOSTNAMELEN
. It is defined in ‘sys/param.h’.
But you can’t count on this to exist, so it is cleaner to handle
failure and try again.
gethostname
stores the beginning of the host name in name
even if the host name won’t entirely fit. For some purposes, a
truncated host name is good enough. If it is, you can ignore the
error code.
The sethostname
function sets the name of the host machine to
name, a string with length length. Only privileged
processes are allowed to do this. Usually it happens just once, at
system boot time.
The return value is 0
on success and -1
on failure.
The following errno
error condition is defined for this function:
EPERM
This process cannot set the host name because it is not privileged.
This function returns the “host ID” of the machine the program is
running on. By convention, this is usually the primary Internet address
of that machine, converted to a long int
. However, some
systems it is a meaningless but unique number which is hard-coded for
each machine.
The sethostid
function sets the “host ID” of the host machine
to id. Only privileged processes are allowed to do this. Usually
it happens just once, at system boot time.
The return value is 0
on success and -1
on failure.
The following errno
error condition is defined for this function:
EPERM
This process cannot set the host name because it is not privileged.
ENOSYS
The operating system does not support setting the host ID. On some systems, the host ID is a meaningless but unique number hard-coded for each machine.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You can use the uname
function to find out some information about
the type of computer your program is running on. This function and the
associated data type are declared in the header file
‘sys/utsname.h’.
The utsname
structure is used to hold information returned
by the uname
function. It has the following members:
char sysname[]
This is the name of the operating system in use.
char nodename[]
This is the network name of this particular computer. In the GNU
library, the value is the same as that returned by gethostname
;
see Host Identification.
char release[]
This is the current release level of the operating system implementation.
char version[]
This is the current version level within the release of the operating system.
char machine[]
This is a description of the type of hardware that is in use.
Some systems provide a mechanism to interrogate the kernel directly for this information. On systems without such a mechanism, the GNU C library fills in this field based on the configuration name that was specified when building and installing the library.
GNU uses a three-part name to describe a system configuration; the three parts are cpu, manufacturer and system-type, and they are separated with dashes. Any possible combination of three names is potentially meaningful, but most such combinations are meaningless in practice and even the meaningful ones are not necessarily supported by any particular GNU program.
Since the value in machine
is supposed to describe just the
hardware, it consists of the first two parts of the configuration name:
‘cpu-manufacturer’. For example, it might be one of these:
"sparc-sun"
,"i386-anything"
,"m68k-hp"
,"m68k-sony"
,"m68k-sun"
,"mips-dec"
The uname
function fills in the structure pointed to by
info with information about the operating system and host machine.
A non-negative value indicates that the data was successfully stored.
-1
as the value indicates an error. The only error possible is
EFAULT
, which we normally don’t mention as it is always a
possibility.
[Top] | [Contents] | [Index] | [ ? ] |
This document was generated on November 5, 2024 using texi2html 5.0.
The buttons in the navigation panels have the following meaning:
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ << ] | FastBack | Beginning of this chapter or previous chapter | 1 |
[ < ] | Back | Previous section in reading order | 1.2.2 |
[ Up ] | Up | Up section | 1.2 |
[ > ] | Forward | Next section in reading order | 1.2.4 |
[ >> ] | FastForward | Next chapter | 2 |
[Top] | Top | Cover (top) of document | |
[Contents] | Contents | Table of contents | |
[Index] | Index | Index | |
[ ? ] | About | About (help) |
where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:
This document was generated on November 5, 2024 using texi2html 5.0.